/*
 * as_lesson_bmp.c
 *
 *  This is a file to show how to use SDL2 to render bitmap in android
 *      Author: Tocy
 *		any questions please contact me by zyvj@qq.com.
 */

#include "as_lesson_log.h"
#include "SDL.h"


//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

int bmp_main(int argc, char *argv[]) 
{
    char *filepath = "/sdcard/hello_world.bmp";
    LOGI("natvie_SDL %s", filepath);

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) == -1) {
        LOGE("SDL_Init failed %s", SDL_GetError());
		return -1;
    }

    LOGI("SDL_CreateWindow");
    SDL_Window * window = SDL_CreateWindow("Hello World!", 100, 100, 640, 480,
            SDL_WINDOW_SHOWN);
    if (window == NULL) {
        LOGE("SDL_CreateWindow failed  %s", SDL_GetError());
    }

    LOGI("SDL_CreateRenderer");
    SDL_Renderer * render = SDL_CreateRenderer(window, -1,
            SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (render == NULL) {
        LOGE("SDL_CreateRenderer failed  %s", SDL_GetError());
    }

    SDL_Surface * bmp = SDL_LoadBMP(filepath);
    if (bmp == NULL) {
        LOGE("SDL_LoadBMP failed: %s", SDL_GetError());
    }

    SDL_Texture * texture = SDL_CreateTextureFromSurface(render, bmp);
    SDL_FreeSurface(bmp);

    SDL_RenderClear(render);
    SDL_RenderCopy(render, texture, NULL, NULL);
    SDL_RenderPresent(render);

    SDL_Delay(2000);

    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(render);
    SDL_DestroyWindow(window);

    //Quit SDL
    SDL_Quit();
	
    return 0;
}
